package com.bxshey.happyTown.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bxshey.happyTown.dto.user.UpdateDto;
import com.bxshey.happyTown.entity.User;
import com.bxshey.happyTown.exception.BusinessException;
import com.bxshey.happyTown.mapper.UserMapper;
import com.bxshey.happyTown.result.ResultEnum;
import com.bxshey.happyTown.service.UserService;
import com.bxshey.happyTown.utils.UploadTheFile;
import com.bxshey.happyTown.vo.user.QueryOpenidVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2024-01-03 22:42:47
 */
@Service("userService")
@Slf4j
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    /**
     * 根据openid查询用户信息
     *
     * @param openID 微信唯一标识ID
     * @return 用户信息
     */
    @Override
    public QueryOpenidVO queryOpenID(String openID) {
        User user = userMapper.queryOpenid(openID);
        QueryOpenidVO queryOpenidVo = new QueryOpenidVO();
        BeanUtils.copyProperties(user, queryOpenidVo);
        return queryOpenidVo;
    }


    /**
     * @param info 需要更新的用户信息
     * @return 更新的成功数
     */
    @Override
    public Integer update(UpdateDto info) {
        User user = new User();
        BeanUtils.copyProperties(info, user);
        System.out.println(user);
        return userMapper.update(user);
    }

    /**
     * 上传头像
     * @param file 图片文件
     * @return 图片路径
     */
    public String updateAvatar(MultipartFile file) {

        UploadTheFile uploadTheFile = new UploadTheFile();
        boolean bool = UploadTheFile.fileFormat(new MultipartFile[]{file});
        List<String> key = new ArrayList<>();
        key.add(UUID.randomUUID().toString());
        List<String> list = uploadTheFile.uploadFile(new MultipartFile[]{file}, key);
        return list.get(0);
    }
}

